package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.MoneyDao;
import com.baor.dao.VipDao;
import com.baor.dao.VipSourceDao;
import com.baor.dao.VipTagDao;
import com.baor.domain.MoneyVo;
import com.baor.domain.UserTagVo;
import com.baor.domain.UserVo;

import com.baor.domain.field.OrderSourceField;
import com.baor.service.VipService;
import com.baor.utils.AjaxResult;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VipServiceImpl implements VipService {

    @Autowired
    private VipDao vipDao;
    @Autowired
    private VipTagDao vipTagDao;
    @Autowired
    private VipSourceDao vipSourceDao;
    @Autowired
    private MoneyDao moneyDao;
    //会员列表
    @Override
    public AjaxResult<?> userList(Map<String, Object> params) {
        params.put("pageIndex", (int) params.get("pageSize") * ((int) (params.get("pageIndex")) - 1));
        params.put("pageSize", params.get("pageSize"));
        Map<String, Object> res = new HashMap<>();
        res.put("userList", vipDao.userList(params));
        res.put("pageCount", vipDao.pageCount(params));
        return AjaxResult.success(res);
    }

    //添加会员
    @Override
    public AjaxResult<?> addUser(UserVo userVo) {
        // 手机号正则表达式
        String telRegex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";

        // 验证手机号格式
        if (!userVo.getUserTel().matches(telRegex)) {
            return AjaxResult.error("请输入正确的手机号");
        }
        LambdaQueryWrapper<UserVo> telQueryWrapper = new LambdaQueryWrapper<>();
        telQueryWrapper.eq(UserVo::getUserTel, userVo.getUserTel());
        int telCount = Math.toIntExact(vipDao.selectCount(telQueryWrapper));

        LambdaQueryWrapper<UserVo> accountQueryWrapper = new LambdaQueryWrapper<>();
        accountQueryWrapper.eq(UserVo::getUserAccount, userVo.getUserAccount());
        int accountCount = Math.toIntExact(vipDao.selectCount(accountQueryWrapper));

        if (telCount > 0) {
            return AjaxResult.error("手机号重复");
        } else if (accountCount > 0) {
            return AjaxResult.error("账号重复");
        } else {
            vipDao.insert(userVo);
            return AjaxResult.success("添加成功");
        }
    }

    //vip标签列表
    @Override
    public AjaxResult<?> vipTagList(Map<String, Object> params) {
        MPJLambdaWrapper<UserTagVo> objectMPJLambdaWrapper = new MPJLambdaWrapper<>();
        objectMPJLambdaWrapper
                .selectAll(UserTagVo.class)
                .eq(UserTagVo::getSiteId, params.get("siteId"))
                .ne(UserTagVo::getDelFlag, 1)
                .orderByAsc(UserTagVo::getUserTagSort);              // 查询为删除的  .ne为不等于
        List<UserTagVo> userTagVos = vipTagDao.selectList(objectMPJLambdaWrapper);
        return AjaxResult.success(userTagVos);
    }

    //修改会员标签
    @Override
    public AjaxResult<?> updateUserTag(UserVo userVo) {
        int row = vipDao.updateById(userVo);
        if (row > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }

    }

    //会员列表
    @Override
    public AjaxResult<?> vipSourceList(OrderSourceField orderSourceField) {
        List<OrderSourceField> orderSourceFields = vipSourceDao.selectList(null);
        return AjaxResult.success(orderSourceFields);
    }


    //通过ID获取用户信息
    @Override
    public AjaxResult<?> getUserInfo(Integer userId) {
        UserVo userInfo = vipDao.getUserInfo(userId);
        return AjaxResult.success(userInfo);
    }

    //会员余额列表
    @Override
    public AjaxResult<?> userBalanceList(Map<String, Object> params) {
        params.put("pageIndex", (int) params.get("pageSize") * ((int) (params.get("pageIndex")) - 1));
        params.put("pageSize", params.get("pageSize"));
        Map<String, Object> res = new HashMap<>();
        res.put("userList", vipDao.userBalanceList(params));
        res.put("pageCount", vipDao.pageCount(params));
        return AjaxResult.success(res);
    }

    //会员余额详情
    @Override
    public AjaxResult<?> userBalanceDetail(Integer moneyId) {
        MPJLambdaWrapper<MoneyVo> userWrapper = new MPJLambdaWrapper<>();
        userWrapper.selectAll(MoneyVo.class)
                .eq(MoneyVo::getMoneyId, moneyId)
                .selectAs(UserVo::getUserAccount, MoneyVo::getUserAccount)
                .leftJoin(UserVo.class, UserVo::getUserId, MoneyVo::getUserId);
        MoneyVo moneyVo = moneyDao.selectOne(userWrapper);
        return AjaxResult.success(moneyVo);
    }

    //计算会员余额总 余额、可提现余额、不可体现余额
    public AjaxResult<?> calculateBalance() {
        UserVo userVo = vipDao.calculateBalance();
        System.out.println(userVo);
        return AjaxResult.success(userVo);
    }

    @Override
    public AjaxResult<?> vipCommissionList(Map<String, Object> params) {
        params.put("pageIndex", (int) params.get("pageSize") * ((int) (params.get("pageIndex")) - 1));
        params.put("pageSize", params.get("pageSize"));
        Map<String, Object> res = new HashMap<>();
        res.put("userList", vipDao.userBalanceList(params));
        res.put("pageCount", vipDao.pageCount(params));
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> getCode() {
        UserVo userVo1 = vipDao.selectList(new LambdaQueryWrapper<UserVo>().orderByDesc(UserVo::getUserId)).get(0);
        return AjaxResult.success("00" + (userVo1.getUserId() + 1));
    }


}
